<?php
/*
 * @Author: your name
 * @Date: 2020-05-21 17:52:58
 * @LastEditTime: 2020-05-25 15:31:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /v3/application/adminapiv3/service/attackLogManage/fileLogManage/SystemEmpowermentService.php
 */ 
namespace app\adminapiv3\service\attackLogManage\fileLogManage;

use app\common\model\Attack;
use app\common\service\Host;

class SystemEmpowermentService extends FileLogService
{
    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-21 17:08:18
     */
    function list ($input)
    {
        $input['host_id']= $this->belongedHost();

        $return = $this->DbList($input);
        $return['data'] = $this->handleData($return['data']);

        return $return;
    }

        /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-08-09 10:11:08
     */
    function Dblist ($filterData)
    {
        $server = db('FileEmpowerment')->alias('fe')
        ->join('host h', 'fe.host_id = h.id')
        ->field('fe.*, h.name');

        !isset($filterData['host_id']) || empty($filterData['host_id']) ? : $server->whereIn('fe.host_id', $filterData['host_id']);
        !isset($filterData['attack_dest']) || empty($filterData['attack_dest']) ? : $server->whereLike('h.name', '%'. $filterData['attack_dest'] .'%');
        !isset($filterData['type']) || empty($filterData['type']) ? : $server->whereLike('fe.type', '%'. $filterData['type'] .'%');
        if (isset($filterData['atime']) && !empty($filterData['atime']))
        {
            $stime = strtotime($filterData['atime']);
            $etime = $stime + 86400;
            $server->whereBetween('fe.atime', [$stime,$etime]);
        }
        !isset($filterData['path']) || empty($filterData['path']) ? : $server->whereLike('fe.path', '%'. $filterData['path'] .'%');

        if (isset($filterData['handle']) && is_numeric($filterData['handle'])) {
            $server->where('handle', $filterData['handle']);
        }

        $return = $server->order('fe.id', 'desc')->paginate($filterData['limit'])->toArray();

        return $return;
    }

    /**
     * @name: 处理数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-09-29 10:58:44
     */
    function handleData ($data)
    {
        $return = [];
        if(empty($data))
        {
            return $return;
        }

        foreach ($data as $k => $v)
        {
            $return[$k] = [
                'id' => $v['id'],
                'attack_dest' => $v['name'],
                'type' => $v['type'],
                'path' => $v['path'],
                'atime' => $v['atime'],
                'ctime' => $v['ctime'],
                'handle' => $this->handle2string($v['handle']) ,
            ];
        }

        return $return;
    }

        /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 10:57:44
     */
    function delete ($id_s)
    {
        $host_id_a = $this->nowGroupHost();
        db('FileEmpowerment')->whereIn('id', $id_s)->whereIn('host_id', $host_id_a)->delete();
    }

    /**
     * @name: 清空
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-05-22 10:57:44
     */
    function clear ()
    {
        $host_id_a = $this->nowGroupHost();
        db('FileEmpowerment')->whereIn('host_id', $host_id_a)->delete();
    }

    /**
     * @name 系统提取数据筛选
     * @author wx
     * @date 2020/5/25 15:08
     */
    function filterData($type){

        //类型
        if (!method_exists($this, $type)) {
            $this->setError($this->CODE_FAIL, '参数错误');
            return false;
        }


        $data = $this->$type();

        return $data;
    }

    /**
     * @name 系统提权筛选数据
     * @author wx
     * @date 2020/6/4 10:13
     */
    function system_empowerment()
    {
        $typeArr = db('ips_type')->where('type', 'network')->select();

        $data['type'] = array();

        foreach ($typeArr as $k => $v){
            $data['type'][$k]['id'] = $v['id'];
            $data['type'][$k]['value'] = $v['show_name'];
        }

        $hostServer = new Host();
        $hostArr = $hostServer->filterData();

        foreach ($hostArr as $key => $val){
            $data['host_id'][$key]['id'] = $val['id'];
            $data['host_id'][$key]['value'] = $val['name'];
        }

        $data['danger_level'] = $this->dangerLevel();

        $data['handle'] = $this->handle();

        return $data;
    }

    /**
     * @name 主动防御筛选数据
     * @author wx
     * @date 2020/6/4 10:43
     */
    function auto_defense()
    {
        $hostId = db('file_attack')->field('host_id, dest_ip')
            ->where('source',1)
            ->group('host_id')
            ->select();

        $addressJson = db('ServerAuth')->where('type', 1)->value('address_json');
        $address = json_decode($addressJson, true);

        array_pop($address);

        foreach ($hostId as $k => $v){
                $data['host_id'][$k]['id'] = $v['host_id'];
                $data['host_id'][$k]['name'] = $v['dest_ip'] . '(' . implode(' ', $address) . ')' ;
        }

        $data['danger_level'] = $this->dangerLevel();

        $data['handle'] = $this->handle();

        return $data;
    }

    /**
     * @name 文件篡改筛选数据
     * @author wx
     * @date 2020/6/4 10:43
     */
    function file_modify()
    {
        $data['handle'] = $this->handle();

        return $data;
    }

    /**
     * @name 文件查杀筛选数据
     * @author wx
     * @date 2020/6/4 10:52
     */
    function file_screening()
    {
        $data['danger_level'] = $this->dangerLevel();
        $data['handle'] = $this->handle();

        return $data;
    }



}